/*
   Copyright 2011 Brook Smith

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package org.xempot.xml;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;

public class RunXmContextEventReader {

	private static XmlContextEventReader getXmlContextEventReader(InputStream in) throws XMLStreamException {
		javax.xml.stream.XMLInputFactory factory = javax.xml.stream.XMLInputFactory.newFactory();
		XMLEventReader reader = factory.createXMLEventReader(in);
		return new XmlContextEventReader(reader);
	}

	XmlContextEventReader reader;

	private static final String HOME_BROOK_SOURCE_QT1_ACT_PUBLIC_2007_0070_XML = "/home/brook/Source/Qt1/act-public-2007-0097.xml";

	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		try {
			FileInputStream in = new FileInputStream(HOME_BROOK_SOURCE_QT1_ACT_PUBLIC_2007_0070_XML);

			XmlContextEventReader contextEventReader = getXmlContextEventReader(in);

			RunXmContextEventReader parse = new RunXmContextEventReader();
			parse.parseFile(contextEventReader);
		} catch (Exception e) {
			e.printStackTrace();
		}
		long endTime = System.currentTimeMillis();
		System.out.println(endTime - startTime);

	}

	void parseFile(XmlContextEventReader reader) throws UnsupportedEncodingException, IOException, XMLStreamException {
		this.reader = reader;

		int count = 0;
		while (reader.hasNext()) {
			XMLEvent event = reader.nextEvent();
			count++;

			switch (event.getEventType()) {
			case XMLStreamConstants.CHARACTERS:
				break;
			case XMLStreamConstants.START_ELEMENT:
				System.out.println(reader.getXPathLocator());
				break;
			default:
				break;
			}
		}
		System.out.println("count = " + count);
	}

}